// 全局变量

var NO_DATA_MSG = '未查询到数据';
var OTHER_INSTRUMENT_NAME = [];//其他器械名称列表

var MY_SM_INSTRUMENT_DISINFECT = (function() {

	// 收集页面查询参数
	function getDetailParams(){
		
		var data = {};

		// data.dateYm = $('#year').val() + '' + $('#month').val();
		data.dtFrom = $('#dtFrom').val();
		data.dtTo = $('#dtTo').val();
		data.institutionId = $('#institutionId').val();;
		data.businessSubType = $('#instrument_disinfect_sec').find('select[name="businessSubType"]').val();// 器械 or 场所
		if ( data.businessSubType == 101 ){
			data.instrumentType = 1;//器械列表code
		}
		// 消毒状态
		var disinfectSt = $('#instrument_disinfect_register').val();
		if ( disinfectSt != '9' ){
			data.disinfectSt = disinfectSt;
		}
		//非全部器械
		if ( $('#instrumentName').val() != 9 ){
			data.instrumentName = data.instrumentName = $('#instrumentName').val();
			if($('#instrumentName').val() == 99){
				//其他器械
				data.otherInstrumentName = OTHER_INSTRUMENT_NAME;
			}
		}
		return data;
	}
	
	/**
	 * 查看明细
	 * */
	function loadInstrumentList(){
		
		WeKnow.loading();
		
		var $selectInstrumentName = $('#instrumentName');
		$selectInstrumentName.empty();
		
		var params = {};
		params.institutionId = $("#institutionId").val();
		
		var businessSubType = $('section#instrument_disinfect_sec').find('select[name="businessSubType"]').val();
		
		if ( businessSubType == '101' ){
			params.type = 1;
			$('.instrument-name').text('器械名称：');
		} else if ( businessSubType == '102' ){
			params.type = 2;
			$('.instrument-name').text('场所名称：');
		}
		
		$.ajax({ 
	        type: "POST", 
	        url: "invs_instrument_disinfect.do/load_instrument", 
	        dataType: "json",
	        data: params,
	        async: true,//异步调用
	        success: function(data) {
	        	WeKnow.closeLoading();
				if (!WeKnow.filterAjaxResult(data)) return false; // session过期check
				
	        	if (data.errcode == 0) {
	        		// 把取到的结果放到
	        		e = '<option value="9">全部</option>';
	        		if ( 'instrumentList' in data && data.instrumentList.length == 0 ){
	        			// WeKnow.errorMsg('未查询到该诊所的器械·场所数据!');
	        			$selectInstrumentName.append(e);
	        			return;
	        		}

	        		$(data.instrumentList).each(function( idx,_a){
	        			// e +='<option value="'+_a.id + '">'+ _a.name + '</option>'
						e +='<option value="'+_a.name + '">'+ _a.name + '</option>'
						OTHER_INSTRUMENT_NAME.push(_a.name);
	        		});
					e += '<option value="99">其他</option>';
	        		$selectInstrumentName.append(e);
	        		
	        	} else if (data.errcode == -1 ) {
	        		WeKnow.errorMsg( '未查询到器械·场所数据!!!' );
	        	} else {
	        		WeKnow.systemMsg('查询失败，请重新登录后再试');
	        	}
	        }
		});
	}
	
	return {
		loadInstrumentList:loadInstrumentList,
		getDetailParams:getDetailParams
	}
})();

$(document).ready(function() {
	
	// 显示图片
	$('#tbodyDisinfectDetailList').on('click','button[name="showImage"]',function(){
		
		var $that = $(this).closest('tr');
		// 模态框显示诊所名，选定的日期
		$('#imageModalTitle').text($('#institution_name_dis').text() + ' ' + $that.data('serial_date'));
		// 显示试纸图片
		MY_SHOW_IMG.createShowImgModal($that.data('institution_id'),$that.data('paper_img_id'));
	});
	
	// 显示申诉图片
	$('#tbodyDisinfectDetailList').on('click','button[name="showAppealAtta"]',function(){
		var $that = $(this);
		var $parentTr = $that.closest('tr');
		
		var appealAtta = $parentTr.data('appeal_atta');
		var institutionId = $('#institutionId').val();
		// 显示交接单图片
		MY_SHOW_IMG.createShowImgModal(institutionId ,appealAtta);
	});
	
	var ACTIVIE_CLASS = 'active_tab_disinfect';
	
	var $tHead  = $('#tHeadDisinfectDetailList');
	var $tbody = $('#tbodyDisinfectDetailList');
	$('section#instrument_disinfect_sec').find('select[name="businessSubType"]').on('change',function(){
		var params = MY_SM_INSTRUMENT_DISINFECT.getDetailParams();
		MY_INSTRUMENT_DISINFECT_COMMON.loadDetailItem(params, $tHead, $tbody);
	});
	
	// 处理申诉 OR 取消异常   注：申诉处理由于多个页面共用，故提炼成appeal.js
	$('#tbodyDisinfectDetailList').on('click','button[name="closeIllegal"],button[name="check"]',function(){
		var $that = $(this);
		var $parentTr = $that.closest('tr');
		
		var illegalId = $parentTr.data('illegal_id');
		var originBtnName= $that.prop('name');
		
		// 更新用模态框
		var params = MY_SM_INSTRUMENT_DISINFECT.getDetailParams( $('#institutionId').val());
		MY_APPEAL.createModalWin
			(illegalId,originBtnName,
				function(){MY_INSTRUMENT_DISINFECT_COMMON.loadDetailItem( params,$tHead,$tbody );},
				function(){ MY_SUMMARIZE.getSummarize();}
			);
	});
	
	
	$('#instrument_disinfect_register,#instrumentName').on('change',function(){
		
		var $tHead  = $('#tHeadDisinfectDetailList');
		var $tbody = $('#tbodyDisinfectDetailList');
		var params = MY_SM_INSTRUMENT_DISINFECT.getDetailParams();
		MY_INSTRUMENT_DISINFECT_COMMON.loadDetailItem(params, $tHead, $tbody);
	});
});